package com.cheng.xbsc.controller;

import com.cheng.xbsc.entity.Address;
import com.cheng.xbsc.entity.User;
import com.cheng.xbsc.entity.vo.Result;
import com.cheng.xbsc.service.AddressService;
import com.cheng.xbsc.service.CarService;
import com.cheng.xbsc.service.OrderService;
import com.cheng.xbsc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private CarService carService;

    @Autowired
    private AddressService addressService;

    /**
     * 检查用户名是否被注册
     *
     * @param username
     * @return
     */
    @GetMapping("/checkUsername/{username}")
    public Result checkUsername(@PathVariable("username") String username) {
        return userService.findByUsername(username);
    }

    /**
     * 检查邮箱是否被注册
     *
     * @param email
     * @return
     */
    @GetMapping("/checkEmail/{email}")
    public Result checkEmail(@PathVariable("email") String email) {
        return userService.findByEmail(email);
    }

    /**
     * 用户注册
     *
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        userService.save(user);
        return new Result(true, "注册成功");
    }

    /**
     * 发送验证码
     *
     * @param email
     * @return
     */
    @GetMapping("/sendCode/{email}")
    public Result sendCode(@PathVariable("email") String email) {
        return userService.sendCode(email);
    }

    /**
     * 修改密码
     *
     * @param code
     * @param user
     * @return
     */
    @PostMapping("/forget/{code}")
    public Result forget(@PathVariable("code") String code, @RequestBody User user) {
        return userService.forget(code, user);
    }

    /**
     * 用户登录
     *
     * @param code
     * @param user
     * @return
     */
    @PostMapping("/login/{code}")
    public Result login(@PathVariable("code") String code, @RequestBody User user) {
        return userService.login(code, user);
    }

    /**
     * 查看用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public Result findDetail(@PathVariable("userId") Integer userId) {
        User user = userService.findById(userId);
        Integer orders = orderService.countAll(userId);
        Integer cars = carService.countByStatus();
        Address address = addressService.findByUserIdAndStatus(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("orders", orders);
        map.put("cars", cars);
        map.put("address", address);
        return new Result(true, "查询成功", map);
    }

    /**
     * 更新头像
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadPic")
    public Result uploadPic(MultipartFile file) throws IOException {
        return userService.uploadPic(file);
    }

    /***
     * 更新用户信息
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        return new Result(true, "更新成功", userService.updateUser(user));
    }

    /**
     * 修改密码
     *
     * @return
     */
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Map<String, String> map) {
        if (map.containsKey("oldPassword") && map.containsKey("newPassword")) {
            return userService.updatePassword(map.get("oldPassword"), map.get("newPassword"));
        }
        return new Result(false, "修改错误");
    }

    /**
     * 用户退出
     */
    @GetMapping("/loginOut")
    public Result loginOut() {
        userService.loginOut();
        return new Result(true, "退出成功");
    }

}
